package per.wsj.stickyheader.layoutmanager

import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearSmoothScroller
import kotlinx.android.synthetic.main.activity_list.*
import per.wsj.lib.StickyLinearLayoutManager
import per.wsj.stickyheader.R
import per.wsj.stickyheader.layoutmanager.model.HeaderItem
import per.wsj.stickyheader.layoutmanager.model.Item
import java.util.*

class LayoutManagerActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)

        val adapter = MyAdapter()
        adapter.setDataList(genDataList())

        rv.layoutManager = StickyLinearLayoutManager(this, adapter)
        rv.adapter = adapter
//        layoutManager.setStickyHeaderListener(object :
//            StickyLinearLayoutManager.StickyHeaderListener {
//            override fun headerAttached(
//                headerView: View?,
//                adapterPosition: Int
//            ) {
//                Log.d("StickyHeader", "Header Attached : $adapterPosition")
//            }
//
//            override fun headerDetached(
//                headerView: View?,
//                adapterPosition: Int
//            ) {
//                Log.d("StickyHeader", "Header Detached : $adapterPosition")
//            }
//        })

//        rv.postDelayed(
//            Runnable { adapter.addDataList(genDataList(adapter.getItemCount())) },
//            5000
//        )
    }

    private fun genDataList(): List<Any>? {
        val items: MutableList<Any> = ArrayList()
        for (i in 0 until 100) {
            if (i % 10 == 0) {
                items.add(HeaderItem("Header $i"))
            } else {
                items.add(Item("Item $i", "description $i"))
            }
        }
        return items
    }

    class TopSmoothScroller(context: Context?) :
        LinearSmoothScroller(context) {
        override fun calculateDtToFit(
            viewStart: Int,
            viewEnd: Int,
            boxStart: Int,
            boxEnd: Int,
            snapPreference: Int
        ): Int {
            return boxStart - viewStart
        }
    }

}